<?php

namespace app\tenant\controller;

use think\Controller;
use Util\SysDb;
use think\Db;

class Authority extends Controller
{
    public function rolelist()
    {
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $tenantid = trim(input('param.tenantid'));
        $tol = (intval($page) - 1) * $limit;
        $list = DB::name("tenant_role")->where(['tenant_id' =>  $tenantid])->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('tenant_role')->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }

    public function rolealllist()
    {
        $list = DB::name("tenant_menu")->select();
        foreach ($list as  $k => &$value) {
            $value['createTime'] = $value['create_time'];
            $value['menuIcon'] = $value['menu_icon'];
            $value['menuName'] = $value['menu_name'];
            $value['parentId'] = $value['parent_id'];
            $value['requestUrl'] = $value['request_url'];
        }
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $list]));
    }

    public function rolegetRoleMenuId()
    {
        $roleId = trim(input('param.roleId'));
        $this->db = new SysDb();
        $count = $this->db->table('tenant_role_menu')->where(['role_id' =>  $roleId])->lists();
        $array = array();
        foreach ($count as  $k => &$value) {
            array_push($array, $value['menu_id']);
        }
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $array]));
    }

    public function rolesetRoleMenu()
    {
        $roleId = trim(input('param.roleId'));
        $menuIds = input('param.menuIds');
        $this->db = new SysDb();
        $count = $this->db->table('tenant_role_menu')->where(['role_id' =>  $roleId])->delete();
        $arrays = array();
        foreach ($menuIds as  $k => &$value) {
            $data['role_id'] = $roleId;
            $data['menu_id'] = $value;
            $data['status'] = 1;
            array_push($arrays, $data);
        }
        $count = $this->db->table('tenant_role_menu')->insertAll($arrays);
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" =>  '修改成功']));
        }
    }

    public function roleupdate()
    {
        $roleName = trim(input('param.role_name'));
        $remark = trim(input('param.remark'));
        $id = trim(input('param.id'));

        $this->db = new SysDb();
        $data = $this->db->table('tenant_role')->where(['id' =>  $id])->item();
        $data['role_name'] = $roleName;
        $data['remark'] = $remark;
        $count = $this->db->table('tenant_role')->where(['id' =>  $id])->update($data);
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
    }

    public function roleupadd()
    {
        $roleName = trim(input('param.role_name'));
        $remark = trim(input('param.remark'));
        $tenantid = trim(input('param.tenantid'));

        $this->db = new SysDb();
        $data['role_name'] = $roleName;
        $data['remark'] = $remark;
        $data['tenant_id'] = $tenantid;
        $count = $this->db->table('tenant_role')->insert($data);
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
    }

    public function roleupdel()
    {
        $id = trim(input('param.id'));
        $this->db = new SysDb();
        $count = $this->db->table('tenant_role')->where(['id' =>  $id])->delete();
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
    }

    public function menuadd()
    {
        $parentId = trim(input('param.parentId'));

        $menuName = trim(input('param.menuName'));
        $requestUrl = trim(input('param.requestUrl'));
        $component = trim(input('param.component'));
        $menuIcon = trim(input('param.menuIcon'));
        $flag = trim(input('param.flag'));

        $this->db = new SysDb();
        $data['parent_id'] = $parentId;

        $data['menu_name'] = $menuName;
        $data['request_url'] = $requestUrl;
        $data['component'] = $component;
        $data['menu_icon'] = $menuIcon;
        $data['flag'] = $flag;

        $count = $this->db->table('tenant_menu')->insert($data);
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
    }

    public function menudel()
    {
        $id = input('param.ids');
        $this->db = new SysDb();
        foreach ($id as  $k => &$value) {
            $count = $this->db->table('tenant_menu')->where(['id' =>  $value])->delete();
        }

        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
    }

    public function menuupdate()
    {
        $parentId = trim(input('param.parentId'));
        $id = trim(input('param.id'));
        $menuName = trim(input('param.menuName'));
        $requestUrl = trim(input('param.requestUrl'));
        $component = trim(input('param.component'));
        $menuIcon = trim(input('param.menuIcon'));
        $flag = trim(input('param.flag'));

        $this->db = new SysDb();
        $data = $this->db->table('tenant_menu')->where(['id' =>  $id])->item();
        $data['parent_id'] = $parentId;

        $data['menu_name'] = $menuName;
        $data['request_url'] = $requestUrl;
        $data['component'] = $component;
        $data['menu_icon'] = $menuIcon;
        $data['flag'] = $flag;

        $count = $this->db->table('tenant_menu')->where(['id' =>  $id])->update($data);
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
    }

    public function accountlist()
    {
        $tenantid = trim(input('param.tenantid'));
        $status = trim(input('param.status'));
        $page = trim(input('param.pageNo'));
        $where=array(
            "ai.status" => $status,
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $limit = trim(input('param.pageSize'));
        $tol = (intval($page) - 1) * $limit;
        $list = Db::table('tenant_account')
            ->alias('ai')
            ->join('tenant_role ar', 'ai.role_id = ar.id', "left")
            ->where(['ai.tenant_id' =>  $tenantid])
            ->where($where)
            ->limit($tol, $limit)
            ->order("ai.id")
            ->field(array(' ai.id' => 'aiid', 'ai.create_time' => 'aicreate_time', 'ai.status' => 'aistatus', 'ai.remark' => 'airemark', 'ai.account' => 'aiaccount', 'ar.role_name' => 'arrole_name', 'ai.login_time' => 'ailogin_time', 'ai.role_id' => 'airole_id'))
            ->select();
        $count = Db::table('tenant_account')
            ->alias('ai')
            ->join('tenant_role ar', 'ai.role_id = ar.id', "left")
            ->where(['ai.tenant_id' =>  $tenantid])
            ->where($where)
            ->order("ai.id ")
            ->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }

    public function accountdel()
    {
        $id = input('param.id');
        $this->db = new SysDb();
        $count = $this->db->table('tenant_account')->where(['id' =>  $id])->delete();
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
    }

    public function accountfreezeOrThaw()
    {
        $id = input('param.id');
        $status = input('param.status');
        $this->db = new SysDb();
        $data['status'] = $status;
        $count = $this->db->table('tenant_account')->where(['id' =>  $id])->update($data);
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
    }
    public function accountupdate()
    {
        $id = trim(input('param.id'));;
        $roleId = trim(input('param.roleId'));
        $account = trim(input('param.account'));
        $remark = trim(input('param.remark'));;
        $password = md5(trim(input('param.password')));
        $this->db = new SysDb();
        $data= $this->db->table('tenant_account')->where(['id' =>  $id])->item();
        // $data['role_id']= $roleId;
        // $data['account']= $account;
        // $data['remark']= $remark;
        // $data['is_store_manager']= $isStoremanager;
        // $data['password']= $password;
        // $data['store_id']= $storeId;
        if ($password == "") {
            
                $data['role_id']= $roleId;
                $data['account']= $account;
                $data['remark']= $remark;
               
                $count = $this->db->table('tenant_account')->where(['id' =>  $id])->update($data);
                if ($count) {
                    exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
                }
        } else {
            
                $data['role_id']= $roleId;
                $data['account']= $account;
                $data['remark']= $remark;
                $data['password']= $password;
                $count = $this->db->table('tenant_account')->where(['id' =>  $id])->update($data);
                if ($count) {
                    exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
                }
        }
    }

    public function accountadd()
    {
        $tenantid = trim(input('param.tenantid'));
        $roleId = trim(input('param.roleId'));
        $account = trim(input('param.account'));
        $remark = trim(input('param.remark'));;
        $password = md5(trim(input('param.password')));
        $this->db = new SysDb();
            
                $data['role_id']= $roleId;
                $data['account']= $account;
                $data['remark']= $remark;
                $data['tenant_id']= $tenantid;
                $data['password']= $password;
                $data['status']= 1;
                $count = $this->db->table('tenant_account')->insert($data);
                if ($count) {
                    exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
                }
           
        
    }
}
